﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace JSales.ModuleSystem
{
    public partial class ModifyPswdForm : Form
    {
        public ModifyPswdForm()
        {
            InitializeComponent();
        }

        private void BtCloseClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btComfirm_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textPassOld.Text)||string.IsNullOrEmpty(textPass1.Text)||string.IsNullOrEmpty(textPass2.Text))
            {
                MessageBox.Show("原密码、新密码都不能为空！");
            }
            else
            {
                if(GlobalVar.cUser.PSWD==textPassOld.Text)
                {
                    ChangePswdClass change = new ChangePswdClass();
                    
                    if (change.Changepassword(GlobalVar.cUser.UserID, textPass1.Text)==true)
                    {
                        MessageBox.Show("密码修改成功！");
                    }
                    else
                    {
                        MessageBox.Show("密码修改过程中发生异常!");
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("原密码不正确！");
                }
            }
        }

        private void TextPass2TextChanged(object sender, EventArgs e)
        {
            if(textPass1.Text.ToString()!=textPass2.Text.ToString())
            {
                errorPassMatch.SetIconAlignment(this.textPass2,ErrorIconAlignment.MiddleRight);
                errorPassMatch.SetIconPadding(this.textPass2,2); 
                errorPassMatch.SetError(this.textPass2,"两次输入密码不匹配");
                btComfirm.Enabled = false;
            }
            else
            {
                errorPassMatch.SetError(this.textPass2, "");
                btComfirm.Enabled = true;
            }


        }

        private void ModifyPswdFormLoad(object sender, EventArgs e)
        {
            btComfirm.Enabled = false;
            if(textPassOld.Text==null||textPassOld.Text=="")
            {
                errorPassMatch.SetError(this.textPassOld,"请输入原密码");
            }
        }

        private void TextPassOldTextChanged(object sender, EventArgs e)
        {
            if (textPassOld.Text == null || textPassOld.Text == "")
            {
                errorPassMatch.SetError(this.textPassOld, "请输入原密码");
            }
            else
            {
                errorPassMatch.SetError(this.textPassOld, "");
            }
        }
    }
}
